# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2022-present JELOS (https://github.com/JustEnoughLinuxOS)

#Set mupen64-plus-sa config for R33S
if [ ! -d "/storage/.config/mupen64plus" ]; then
    mkdir -p "/storage/.config/mupen64plus/"
    cp -r /usr/local/share/mupen64plus/mupen64plus.cfg* /storage/.config/mupen64plus/
fi
if [ -f "/storage/.config/mupen64plus/mupen64plus.cfg.rgarc" ]; then
  rm /storage/.config/mupen64plus/mupen64plus.cfg
  mv /storage/.config/mupen64plus/mupen64plus.cfg.rgarc /storage/.config/mupen64plus/mupen64plus.cfg
fi
if [ ! -f "/storage/.config/mupen64plus/custominput.ini" ]; then
cat <<EOF >/storage/.config/mupen64plus/custominput.ini
[rg_arc_joypad]
plugged = True
mouse = False
AnalogDeadzone = 0,0
AnalogPeak = 32768,32768
DPad R = button(15)
DPad L = button(14)
DPad D = button(13)
DPad U = button(12)
Start = button(11)
Z Trig = button(8)
B Button = button(3)
A Button = button(1)
C Button R = button(2)
C Button L = button(4)
C Button D = button(0)
C Button U = button(5)
R Trig = button(7)
L Trig = button(6)
Mempak switch = 
Rumblepak switch = 
# Analog axis configuration mappings
X Axis = 
Y Axis = 
EOF
fi

#Set drastic-sa config
if [ ! -d "/storage/.config/drastic" ]; then
    mkdir -p "/storage/.config/drastic"
    cp -r "/usr/config/drastic" "/storage/.config/"
fi
if [ -f "/storage/.config/drastic/config/drastic.cfg.arc" ]; then
  mv /storage/.config/drastic/config/drastic.cfg.arc /storage/.config/drastic/config/drastic.cfg
fi

# Copy over device specific retroarch remappings unless they already exist
REMAP_DIR="/usr/lib/autostart/quirks/devices/${QUIRK_DEVICE}/remappings"
if [ -d "${REMAP_DIR}" ]; then
    for dir in "${REMAP_DIR}"/*/; do
        CORE_NAME=$(basename "${dir}")
        if [ ! -d "/storage/remappings/${CORE_NAME}" ]; then
            cp -r "${dir}" "/storage/remappings/"
        fi
    done
fi

# hypseus aka daphne
if [ ! -d "/storage/.config/game/configs/hypseus/" ]; then
    mkdir -p /storage/.config/game/configs/hypseus/
fi
if [ ! -f "/storage/.config/game/configs/hypseus/hypinput.ini" ]; then
cat <<EOF >/storage/.config/game/configs/hypseus/hypinput.ini
# Sample hypinput.ini
# All key options listed
# Arcade layout by OzFalcon
# Hypseus uses SDL2 Keycodes
# KEY_BUTTON3 Turns scoreboard on/off
# SWITCH = Key1 Key2 Joy
# Start + Select will exit game
##################################
#Button Info for ARC-D running ArchR by Morfious
#
#Controller Button		ID#		Game Function
#Dpad Up			13		Dpad Up			1073741906		SDLK_UP		BUTTON_DPAD_UP
#Dpad Down			14		Dpad Down		1073741905		SDLK_DOWN	BUTTON_DPAD_DOWN
#Dpad Left			15		Dpad Left		1073741904		SDLK_LEFT	BUTTON_DPAD_LEFT
#Dpad Right			16		Dpad Right		1073741903		SDLK_RIGHT	BUTTON_DPAD_RIGHT
#Select				11		Insert Coin
#Start				12		Start Game
#A Button			2		Button 1
#B Button			1		Button 2
#C Button			3		Toggle Scoreboard
#X Button			4		Skill 1
#Y Button			5		Skill 2
#Z Button			6		Skill 3
#L1				7		Pause
#R1				8		Test Mode
#L2				9		Reset
#R2				10		Service Mode

[KEYBOARD]
KEY_UP = 1073741906 SDLK_UP 13
KEY_DOWN = 1073741905 SDLK_DOWN 14
KEY_LEFT = 1073741904 SDLK_LEFT 15
KEY_RIGHT = 1073741903 SDLK_RIGHT 16
KEY_COIN1 = SDLK_5 0 11
KEY_COIN2 = SDLK_6 0 0
KEY_START1 = SDLK_1 0 12
KEY_START2 = SDLK_2 0 0
KEY_BUTTON1 = SDLK_LCTRL 0 2
KEY_BUTTON2 = SDLK_LALT 0 1
KEY_BUTTON3 = SDLK_SPACE 0 3
KEY_SKILL1 = SDLK_LSHIFT 0 4
KEY_SKILL2 = SDLK_z 0 5
KEY_SKILL3 = SDLK_x 0 6
KEY_SERVICE = SDLK_9 0 10
KEY_TEST = SDLK_F2 SDLK_F4 8
KEY_RESET = SDLK_0 0 9
KEY_SCREENSHOT = SDLK_F12 0 0
KEY_QUIT = SDLK_ESCAPE 0 0
KEY_PAUSE = SDLK_p 0 7
KEY_CONSOLE = SDLK_BACKSLASH 0 0
KEY_TILT = SDLK_t 0 0
END
EOF
fi

# PPSSPP controls
SOURCE_DIR="/usr/config/ppsspp"
CONF_DIR="/storage/.config/ppsspp"
# Check if conf dir exists
if [ ! -d "${CONF_DIR}" ]
then
  cp -rf ${SOURCE_DIR} ${CONF_DIR}
  CONTROLS_INI="${CONF_DIR}/PSP/SYSTEM/controls.ini"
  cat <<EOF >${CONTROLS_INI}
[ControlMapping]
Up = 10-19
Down = 10-20
Left = 10-21
Right = 10-22
Cross = 10-190
Circle = 10-189
Square = 10-188
Triangle = 10-191
Start = 10-197
Select = 10-196
L = 10-193
R = 10-192
An.Up = 10-4003
An.Down = 10-4002
An.Left = 10-4001
An.Right = 10-4000
Pause = 10-4,10-106
Save State = 10-4010
Load State = 10-4008
EOF
fi

